import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  namespaced: true,
  state: {
    // token
    user: localStorage.getItem('user'),
    token: localStorage.getItem('token'),
    search: ''
  },

  getters: {},
  mutations: {
    //保存用户信息
    SET_USER: (state, data) => {
      state.user = data
      state.token = JSON.parse(data).token
      window.localStorage.setItem('user', data)
      window.localStorage.setItem('token', state.token)
    },

    //删除用户信息
    DEL_USER: (state) => {
      state.user = null;
      state.token = null;
      window.localStorage.removeItem('user')
      window.localStorage.removeItem('token')
    },

    SET_SEARCH: (state, search) => {
      state.search = search
    },

  },
  actions: {
    async setUser ({ commit }, user) {
      return new Promise((resolve) => {
        commit('SET_USER', user);
        resolve();
      });
    }
  },
  modules: {}
})
